package org.linphone.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import org.linphone.db.SipConfOpenHelper;
import org.linphone.db.domain.ConfInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xiang on 2017/12/1.
 */

public class SipConfDao {
	private SipConfOpenHelper sipConfOpenHelper;

	//私有化构造方法
	private SipConfDao(Context context){
		//创建数据库
		sipConfOpenHelper = new SipConfOpenHelper(context);
	}

	//申明一个当前类的对象
	private static SipConfDao sipConfDao = null;
	//提供一个静态方法
	public static SipConfDao getInstance(Context context){
		if(sipConfDao == null){
			sipConfDao = new SipConfDao(context);
		}

		return sipConfDao;
	}

	//增加一个条目
	public void insert(String state,String username,String psd,String domain,String musicbg,String broadcastnum,String broadcastarea){
		SQLiteDatabase db = sipConfOpenHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("state",state);
		values.put("username",username);
		values.put("psd",psd);
		values.put("domain",domain);
		values.put("musicbg",musicbg);
		values.put("broadcastnum",broadcastnum);
		values.put("broadcastarea",broadcastarea);

		db.insert("collection",null,values);
		db.close();
	}

	//判断是否有数据
	public boolean isHasData(){
		SQLiteDatabase db = sipConfOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("collection",new String[]{"state"},null,null,null,null,"_id asc");
		while(cursor.moveToNext()){
			cursor.close();
			db.close();

			return true;
		}
		cursor.close();
		db.close();
		return false;
	}

	//修改数据
	public void update(String state,String username,String psd,String domain,String musicbg,String broadcastnum,String broadcastarea){
		SQLiteDatabase db = sipConfOpenHelper.getWritableDatabase();

		ContentValues values = new ContentValues();

		values.put("username",username);
		values.put("psd",psd);
		values.put("domain",domain);
		values.put("musicbg",musicbg);
		values.put("broadcastnum",broadcastnum);
		values.put("broadcastarea",broadcastarea);

		db.update("collection",values,"state = ?",new String[]{state});
		db.close();

	}

	//查询数据
	public List<ConfInfo> findAll(){
		SQLiteDatabase db = sipConfOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("collection",new String[]{"state","username","psd","domain","musicbg","broadcastnum","broadcastarea"},null,null,null,null,"_id asc");
		List<ConfInfo> confList = new ArrayList<>();
		while (cursor.moveToNext()){
			ConfInfo conf = new ConfInfo();
			conf.state = cursor.getString(0);
			conf.username = cursor.getString(1);
			conf.psd = cursor.getString(2);
			conf.domain = cursor.getString(3);
			conf.musicbg = cursor.getString(4);
			conf.broadcastnum = cursor.getString(5);
			conf.broadcastarea = cursor.getString(6);
			confList.add(conf);
		}
		cursor.close();
		db.close();

		return confList;
	}

	//删除数据
	public void delete(String state){
		SQLiteDatabase db = sipConfOpenHelper.getWritableDatabase();
		db.delete("collection","state = ?",new String[]{state});
		db.close();
	}



}
